﻿using Himp.Base;
using Himp.DataHub;
using Himp.Platform;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Dynamic.Core;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.Application.Dtos;
using Volo.Abp.Domain.Repositories;
using Volo.Abp.ObjectMapping;

namespace Himp.ShareData
{
    /// <summary>
    /// 公共项目服务
    /// </summary>
    public class PubItemService : PlatformAppService, IPubItemService
    {
        private readonly IRepository<PubItem, Guid> _pubItemRepository;
        private IBizSeqService _bizSeqService;

        public PubItemService(IRepository<PubItem, Guid> pubItemRepository, IBizSeqService bizSeqService)
        {
            _pubItemRepository = pubItemRepository;
            _bizSeqService = bizSeqService;
        }

        /// <summary>
        /// 获取公共收费项目列表
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public async Task<PagedResultDto<PubItemDto>> GetListAsync(PubItemInput input)
        {
            var iQuery = await _pubItemRepository.GetQueryableAsync();

            if (input.Pi == 0)
            {
                var itemsDto = ObjectMapper.Map<List<PubItem>, List<PubItemDto>>(iQuery.ToList());

                return new PagedResultDto<PubItemDto>(itemsDto.Count, itemsDto);
            }
            else
            {
                var totalCount = iQuery.Count();
                var items = iQuery.Page(input.Pi, input.Ps);
                var itemsDto = ObjectMapper.Map<List<PubItem>, List<PubItemDto>>(items.ToList());

                return new PagedResultDto<PubItemDto>(totalCount, itemsDto);
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<PubItemDto> GetAsync(Guid id)
        {
            var publicChargeItem = await _pubItemRepository.GetAsync(id);
            var itemDto = ObjectMapper.Map<PubItem, PubItemDto>(publicChargeItem);

            return itemDto;
        }

        /// <summary>
        /// 添加公共收费项目
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public async Task<PubItemDto> PostAsync(PubItemCreateDto input)
        {
            var pubItem = ObjectMapper.Map<PubItemCreateDto, PubItem>(input);
            var format = "{0:" + input.PubItemCatgeCodg + "000000}";
            var pubItemCodg = await _bizSeqService.GetAsync(nameof(PubItem) + input.PubItemCatgeCodg, format);

            if (string.IsNullOrEmpty(pubItem.PubItemCodg))
            {
                pubItem.PubItemCodg = pubItemCodg;
            }

            pubItem.ChrgitmType = new PubItemAssocChrgitmType();
            pubItem.ChrgitmType.ClncChrgitmTypeCode = input.ClncChrgitmTypeCode;
            pubItem.ChrgitmType.ClncChrgitmTypeName = input.ClncChrgitmTypeName;

            pubItem = await _pubItemRepository.InsertAsync(pubItem);
            var itemDto = ObjectMapper.Map<PubItem, PubItemDto>(pubItem);

            return itemDto;
        }

        /// <summary>
        /// 修改公共收费项目
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public async Task<PubItemDto> PutAsync(Guid id, PubItemUpdateDto input)
        {
            var publicChargeItem = await _pubItemRepository.GetAsync(id);
            publicChargeItem = ObjectMapper.Map(input, publicChargeItem);
            var itemDto = ObjectMapper.Map<PubItem, PubItemDto>(publicChargeItem);

            return itemDto;
        }

        /// <summary>
        /// 更新公共收费项目状态
        /// </summary>
        /// <returns></returns>
        public async Task PostStatusAsync(List<PublicItemStatusUpdateDto> input)
        { 
            foreach (var item in input)
            {
                var publicChargeItem = await _pubItemRepository.GetAsync(item.Id);

                if (item.IsDeactivate.HasValue)
                {
                    publicChargeItem.SetDeactivate(item.IsDeactivate.Value);
                }

                if (item.IsApproval.HasValue)
                {
                    publicChargeItem.SetApproval(item.IsApproval.Value);
                }
            }
        }

        /// <summary>
        /// 删除公共收费项目
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task DeleteAsync(Guid id)
        {
            await _pubItemRepository.DeleteAsync(id);
        }
    }
}
